#include "TimTask.h"
#include "MY_Process.h"
#include "MYusart.h"
#include "tim.h"
#include "Motor.h"

#define TASKS_MAX 8

uint8_t Sleep_Flag = 0;

extern uint8_t Flag_ADC;
extern uint16_t ADC_Value;

static TASK_COMPONENTS TaskComps[] =
    {
        {0, ITVTIME_OLED, ITVTIME_OLED, Task_OLED_Upadata},
        {0, ITVTIME_UART, ITVTIME_UART, Task_UART_Print},
        {0, ITVTIME_LED, ITVTIME_LED, Task_LED},
        {0, ITVTIME_ADC, ITVTIME_ADC, Task_ADC},
        {0, ITVTIME_KEY, ITVTIME_KEY, Task_Key},
        {0, ITVTIME_NRF, ITVTIME_NRF, Task_NRF},
        {0, ITVTIME_MOTO, ITVTIME_MOTO, Task_MOTO},
        {0,1000, ITVTIME_AICAR, Task_AiCar},
};

void TaskRemarks(void)
{
  uint8_t i;
  for (i = 0; i < TASKS_MAX; i++)
  {
    if (TaskComps[i].Timer)
    {
      TaskComps[i].Timer--;
      if (TaskComps[i].Timer == 0)
      {
        TaskComps[i].Timer = TaskComps[i].ItvTime;
        TaskComps[i].Run = 1;
      }
    }
  }
}

void TaskProcess(void)
{
  uint8_t i;
  for (i = 0; i < TASKS_MAX; i++)
  {
    if (TaskComps[i].Run)
    {
      TaskComps[i].TaskHook();
      TaskComps[i].Run = 0;
    }
  }
}

void Task_TtvTime_Set(u8 TaskNum, u16 ItvTime)
{
  TaskComps[TaskNum].ItvTime = ItvTime;
}

void Task_Timer_Set(u8 TaskNum, u16 Timer)
{
  TaskComps[TaskNum].Timer = Timer;
}

u16 Task_Timer_Get(u8 TaskNum)
{
  return TaskComps[TaskNum].Timer;
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  if (htim == (&TASK_TIM))
  {
    TaskRemarks();
  }

  if (htim == (&MOTOR_TIM))
  {
    Moto_Tim_Service();
  }
}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
  ADC_Value = HAL_ADC_GetValue(&hadc1);
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
}


